<?php
class PM_z
{

    /**
     * 轮询解散倒计时（自动解散触发）
     * @param $room_id
     * @param $db_connect
     * @return bool|int
     */
    public static function __auto_cancel( $room_id , $db_connect ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //房间是否游戏状态；如果非正常游戏状态，则返回999；前端处理为已解散（即提示房间已被解散，返回大厅）
        if( $room_info == false || $room_info == null || $room_info['room_status'] != 1 ){return 998;}
        //是否处于解散状态(返回false)
        if( $room_info['diss_ing'] != 1 ){
            return false;
        }else{
            $diss_c_time = ($room_info['diss_time'] + 300) - time();
            if( $diss_c_time < 0 ){//如果倒计时都已经是负数了，那么就应该要执行到自动解散了；
                //执行解散操作
                self::__send_DissLog( $room_info , $room_info['diss_uid'] , $db_connect );
                return 999;
            }else{
                return $diss_c_time;
            }
        }
    }

    /**
     * 用户申请解散房间
     * @param $room_id
     * @param $user_id
     * @param $db_connect
     * @return bool
     */
    public static function __index( $room_id , $user_id , $db_connect ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //正在进行中的游戏申请解散只能是正在进行中的游戏申请，状态为1的申请，否则不能申请
        if( $room_info['room_status'] != 1 ){
            return false;
        }
        if( $room_info['diss_ing'] == 1 ){//当前房间正在申请解散,不能重复申请
            return false;
        }
        //修改房间的diss_ing信息；代表当前正在申请解散(默认解散人数为1)
        $cols = array();
        $cols['diss_ing']   = 1;//设置房间处于解散状态
        $cols['diss_count'] = 1;//当前同意解散人数（默认为1）
        $cols['diss_time']  = time();//解散开始时间
        $cols['diss_uid']   = $user_id;
        $cols['diss_id']    = date('His').rand(100000,999999);
        $db_connect->update('tt_room')->cols( $cols )->where('room_id=' . $room_id )->query();
        //向所有用户发送申请解散LOG
        $temp = array();
        $temp['pl']          = 26;//玩家申请解散
        $temp['diss_id']     = $cols['diss_id'];
        PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        return true;
    }

    /**
     * 用户取消解散
     * @info 这里为了安全起见；如果状态已经是已结束，是不能取消了，可能他点晚了
     * @param $room_id
     * @param $user_id
     * @param $db_connect
     */
    public static function __cancel( $room_id , $user_id , $db_connect ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        if( $room_info['room_status'] != 1 ){
            return false;
        }
        //取消解散记录变量
        $cols = array();$cols['diss_ing']        = 0;$cols['diss_count']      = 0;$cols['diss_fail_count'] = 0;$cols['diss_time']  = 0;$cols['diss_uid']   = 0;$cols['diss_id'] = '';
        $db_connect->update('tt_room')->cols( $cols )->where('room_id=' . $room_id)->query();//修改解散状态
        //发送取消日志(这个日志其实就是拒绝成功的一样的日志)
        $temp = array();
        $temp['pl']          = 28;
        $temp['fail']        = true;//如果这里是true，则代表本次申请失败了，false则是一个人的拒绝
        $temp['diss_id']     = $room_info['diss_id'];
        PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        return true;
    }

    /**
     * 用户拒绝解散
     * @info：如果当前拒绝人数达到2人，即不可进行解散操作；牌局继续
     * @param $room_id
     * @param $user_id
     * @param $db_connect
     */
    public static function __fail(  $room_id , $user_id , $db_connect  ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //是否已经有2个人拒绝了，有的话，进行拒绝LOG发送
        if( $room_info['diss_fail_count'] >= 2 ){
            $temp = array();
            $temp['pl']          = 28;
            $temp['fail']        = true;//如果这里是true，则代表本次申请失败了，false则是一个人的拒绝
            $temp['diss_id']     = $room_info['diss_id'];
            $cols = array();$cols['diss_ing']        = 0;$cols['diss_count']      = 0;$cols['diss_fail_count'] = 0;$cols['diss_time']  = 0;$cols['diss_uid']   = 0;$cols['diss_id'] = '';
            $db_connect->update('tt_room')->cols( $cols )->where('room_id=' . $room_id)->query();//修改解散状态
            PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        }
        //进行拒绝人数修改操作
        $db_connect->update('tt_room')->cols(array('diss_fail_count' => ($room_info['diss_fail_count']+1) ))->where('room_id=' . $room_id)->query();
        //如果加上当前拒绝人数，达到了2个人，则拒绝此次申请解散
        if( ($room_info['diss_fail_count']+1) >= 2 ){
            $temp = array();
            $temp['pl']          = 28;
            $temp['fail']        = true;//如果这里是true，则代表本次申请失败了，false则是一个人的拒绝
            $temp['diss_id']     = $room_info['diss_id'];
            $cols = array();$cols['diss_ing']        = 0;$cols['diss_count']      = 0;$cols['diss_fail_count'] = 0;$cols['diss_time']  = 0;$cols['diss_uid']   = 0;$cols['diss_id'] = '';
            $db_connect->update('tt_room')->cols( $cols )->where('room_id=' . $room_id)->query();//修改解散状态
            PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        }else{
            $temp = array();
            $temp['pl']          = 28;
            $temp['fail']        = false;//如果这里是true，则代表本次申请失败了，false则是一个人的拒绝
            $temp['diss_id']     = $room_info['diss_id'];
            PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        }
        return true;
    }

    /**
     * 用户同意解散
     * @info:如果当前达到3人，即可进行解散操作（已进行第二局的解散不返回钻石，正在第一局的进行的解散可以返回钻石）
     * @param $room_id
     * @param $user_id
     * @param $db_connect
     */
    public static function __success(  $room_id , $user_id , $db_connect  ){
        //取出房间信息
        $room_info  = $db_connect->select('*')->from('tt_room')->where("room_id= '". $room_id ."' ")->row();
        //是否人数已达到2人，已达到的话，直接进行解散LOG发送 - 3人房间，拒绝人数只要2个人即可
        if( $room_info['diss_count'] >= 2 ){
            self::__send_DissLog( $room_info , $user_id , $db_connect );
            return true;
        }
        //如果人数没有达到3人，则修改room中的diss_count + 1；然后下发有用户同意的log通知
        $db_connect->update('tt_room')->cols(array('diss_count' => ($room_info['diss_count']+1) ))->where('room_id=' . $room_id)->query();
        //下发用户同意的LOG通知(同意解散)
        $temp = array();
        $temp['pl']      = 27;
        $temp['diss_id'] = $room_info['diss_id'];
        PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        //用户同意解散后是否达到了3人，达到3人也同时发送正式解散的log
        if( ($room_info['diss_count']+1) >= 2 ){
            self::__send_DissLog( $room_info , $user_id , $db_connect );
        }
        return true;
    }

    /**
     * 正式解散了，日日日日~~~~~~~~~~~~~~~~~~~~~~~~~
     * @param $room_id
     * @param $user_id
     * @param $db_connect
     */
    public static function __send_DissLog( $room_info , $user_id , $db_connect ){
        //当前进行到了第几局？,只有第一局的解散才会返还钻石
        if( $room_info['room_js'] == 1 ){
            if( $room_info['consume'] >= 1 ){//假如扣除了钻石的话，则对开房人进行钻石恢复
                PHZ_Member::__UserAdd_consume( $room_info['room_cid'] , $room_info['consume'] , $db_connect );
            }
        }
        //修改房间信息，本局已结束
        $cols = array();
        $cols['diss_count']  = 0;$cols['diss_ing']    = 0;$cols['diss_time']    = 0;$cols['diss_uid']   = 0;$cols['diss_fail_count'] = 0;$cols['diss_id'] = '';
        $cols['room_status'] = 2;$cols['room_end_time'] = date('Y-m-d H:i:s');
        $db_connect->update('tt_room')->cols( $cols )->where('room_id=' . $room_info['room_id'] )->query();
        //发送解散日志,日志中应该要包含房间结算数据
        $temp = array();
        $temp['pl']          = 29;
        $temp['diss_id']     = $room_info['diss_id'];
        $temp['js']          = $temp['js'] = PM_f::__index( $room_info['room_id'] , $db_connect );
        PHZ_Log::__send_user_log( $room_info , $user_id , $temp , $db_connect );
        return true;
    }

}